package work.person;


//将所有的Excel文件称读取为json,并写入文件

import ding.utils.ExcelUtils;
import javafx.util.Pair;
import org.apache.poi.ss.usermodel.CellType;
import work.config.Constant;
import work.fram.FileGroup;
import work.fram.GroupsWrapper;
import work.person.helper.LoggerHelper;

import java.io.File;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Map;

public class Handle {

    public static void main(String[] args)  {

        Map<String, String> map = NameIdcardReaname.map();

        map.forEach((old,now)->{

            File file = new File(Constant.src, old);

            now = now.substring(now.lastIndexOf("\\") + 1);
            String[] split = now.split(" +");

            String name = split[0];

            String idcard = split[1];

            //检查是否应该处理该文件夹
            if (!Do.shouldHandle(file, name, idcard)) {
                return;
            }

            File[] files = file.listFiles();

            Map<String, String> m = new HashMap<>();

            //获取所有分组
            GroupsWrapper groupsWrapper = Do.groupsWrapper();

            if (!groupsWrapper.beforeEnter()) {
                return;
            }

            for (FileGroup group : groupsWrapper.getFileGroups()) {
                boolean flag = true;
                String[] list = group.names();

                //遍历文件和分组中的文件
                for (File f : files) {
                    for (String s : list) {
                        if (f.getName().contains(s)) {
                            Map<String, String> r = excelFileToJson(f, name);
                            if (r != null) {
                                String err = map.get("__ERROR_TYPE__");
                                if (err != null) {
                                    //文件中存在错误,不再处理grop
                                    flag = false;
                                    LoggerHelper.log("处理Excel文件时出现错误", name, idcard, err);
                                } else {
                                    m.putAll(r);
                                }
                            }
                            break;
                        }
                    }

                }

                if (flag) {
                    group.handle(m, name, idcard, file);
                }

            }

            groupsWrapper.afterExist();

        });

    }

    private static NumberFormat nf = NumberFormat.getInstance();
    private static Map<String, String> excelFileToJson(File file, String name) {

        try {

            Map<String, String> map = ExcelUtils.transform(file, name, new HashMap<>(), (name1, value) -> {

                if (name1.getCellType() == CellType.STRING) {
                    String n = name1.getStringCellValue();
                    if (value == null) {
                        return null;
                    }

                    String r = null;

                    if (value.getCellType() == CellType.STRING) {
                        r = value.getStringCellValue();
                    } else if (value.getCellType() == CellType.NUMERIC) {
                        r = nf.format(value.getNumericCellValue());
                    } else if (value.getCellType() == CellType.BLANK) {
                        r = "";
                    }
                    return new Pair<>(n, r);
                }

                return null;

            });

            return map;

        } catch (Exception e) {
            e.printStackTrace();

            return null;
        }


    }


}
